package cn.ante.org.controller;

import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.org.domain.Department;
import cn.ante.org.query.DepartmentQuery;
import cn.ante.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "部门的api",description="部门相关的crud功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService service;

    /*
    * @Title: loadAll
    * @Description: 分页查询部门所有
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/24 11:49
    * @Parameters: [departmentQuery]
    * @Return cn.ante.basic.utils.PageList<cn.ante.org.domain.Department>
    */
    @PostMapping
    public PageList<Department> loadAll(@RequestBody DepartmentQuery departmentQuery){
        return service.loadAll(departmentQuery);

    }

    /*
    * @Title: loadById
    * @Description: 根据id查询部门信息
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/24 11:51
    * @Parameters: [id]
    * @Return cn.ante.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询部门信息")
    public Department loadById(@ApiParam(name="主键",required = true) @PathVariable("id") Long id){
        return service.loadById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                service.add(department);
            }else{
                service.upddate(department);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }
}
